//
//  MessageViewModel.m
//  CityFire
//
//  Created by Liuyz on 2017/8/15.
//  Copyright © 2017年 刘养柱. All rights reserved.
//

#import "MessageViewModel.h"
#import "UserInfoModel.h"
#import "NSUserDefaults+Tools.h"
#import "MessageModel.h"

@implementation MessageViewModel

- (void)setRemind:(NSDictionary *)parameter
{
    UserInfoModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_USERINFO]];
    if (model) {
        [YQNetworking requestWithUrl:kSetRemind_URL requestType:YQHttpRequestTypePOST refreshRequest:YES cache:NO params:@{@"sessionid" :model.seeeionID, @"username" : model.userName, @"remind" : parameter[@"remind"], @"sound" : parameter[@"sound"], @"vibrate" : parameter[@"vibrate"]} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
            //        username=admin&remind=1&sound=1&vibrate=1
            
        } successBlock:^(id response, NSError *responseInfo) {
            
            self.responseInfo = responseInfo;
            
            if (responseInfo.code == 0) {
                [NSUserDefaults writeUserDataWithKey:parameter[@"remind"] forKey:kNSUSERDEFAULT_KEY_ISMINKD];
                [NSUserDefaults writeUserDataWithKey:parameter[@"sound"] forKey:kNSUSERDEFAULT_KEY_ISSOUND];
                [NSUserDefaults writeUserDataWithKey:parameter[@"vibrate"] forKey:kNSUSERDEFAULT_KEY_VIBRATE];
                [SVProgressHUD showSuccessWithStatus:@"设置成功"];
            }else {
                [SVProgressHUD showErrorWithStatus:responseInfo.domain];
            }
            
        } failBlock:^(NSError *error) {
            self.requestError = error;
            [SVProgressHUD showErrorWithStatus:error.domain];
        }];
    }
    
    
}


- (void)setFavoriteDays:(NSDictionary *)parameter
{
    UserInfoModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_USERINFO]];
    if (model) {
        [YQNetworking requestWithUrl:kSetXDay_URL requestType:YQHttpRequestTypePOST refreshRequest:YES cache:NO params:@{@"sessionid" :model.seeeionID, @"username" : model.userName, @"x_day" : parameter[@"days"],} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
            //?username =admin&sessionid=xxx&x_day=7
            
        } successBlock:^(id response, NSError *responseInfo) {
            
            NSError *error;
            MessageModel *msgModel = [MTLJSONAdapter modelOfClass:[MessageModel class] fromJSONDictionary:response error:&error];
            
            self.responseInfo = error ? error : responseInfo;
            
            self.messageModel = msgModel;
            
            if (responseInfo.code == 0) {
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_SESSIONID];
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_USERINFO];
                [SVProgressHUD showSuccessWithStatus:@"设置成功"];
                
                [NSUserDefaults writeUserDataWithKey:parameter[@"days"] forKey:kNSUSERDEFAULT_KEY_FAVRITORDAYS];
            }else {
                [SVProgressHUD showErrorWithStatus:responseInfo.domain];
            }
            
        } failBlock:^(NSError *error) {
            self.requestError = error;
            [SVProgressHUD showErrorWithStatus:error.domain];
        }];
    }
    
    
}

- (void)getQueryRealTimeWarningCount:(NSDictionary *)parameter
{
    UserInfoModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_USERINFO]];
    if (model) {
        [YQNetworking requestWithUrl:kQUERY_REAL_WARNINGCOUNT_URL requestType:YQHttpRequestTypePOST refreshRequest:YES cache:NO params:@{@"sessionid" :model.seeeionID, @"username" : model.userName, @"x_day" : [NSString stringWithFormat:@"%d",[[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_FAVRITORDAYS] intValue]-1] ? : @"",} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
            //sername=admin&x_day=30&sessionid=1
        } successBlock:^(id response, NSError *responseInfo) {
            
            //        response[@"waringSize"];
            if (responseInfo.code == 0) {
                
            }else {
                [SVProgressHUD showErrorWithStatus:responseInfo.domain];
            }
            
        } failBlock:^(NSError *error) {
            self.requestError = error;
            [SVProgressHUD showErrorWithStatus:error.domain];
        }];
    }
    
}

@end
